package com.example.demo.controller;

import com.example.demo.model.Category;
import com.example.demo.model.TextDocument;
import com.example.demo.service.CategoryService;
import com.example.demo.service.TextDocumentService;
import com.example.demo.service.UserService;
import com.example.demo.service.WebLinkService;
import com.example.demo.service.SiteSettingsService;
import com.example.demo.service.LinkCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Optional;

@Controller
public class HomeController {

    private final TextDocumentService textDocumentService;
    private final CategoryService categoryService;
    private final UserService userService;
    private final WebLinkService webLinkService;
    private final SiteSettingsService siteSettingsService;
    private final LinkCategoryService linkCategoryService;

    @Autowired
    public HomeController(TextDocumentService textDocumentService, CategoryService categoryService, UserService userService, WebLinkService webLinkService, SiteSettingsService siteSettingsService, LinkCategoryService linkCategoryService) {
        this.textDocumentService = textDocumentService;
        this.categoryService = categoryService;
        this.userService = userService;
        this.webLinkService = webLinkService;
        this.siteSettingsService = siteSettingsService;
        this.linkCategoryService = linkCategoryService;
    }

    @GetMapping("/")
    public String home(Model model, @RequestParam(required = false) Long categoryId) {
        // 获取所有分类
        List<Category> categories = categoryService.findAllCategories();
        model.addAttribute("categories", categories);
        
        // 如果指定了分类ID，则获取该分类下的文档
        List<TextDocument> documents;
        Category selectedCategory = null;
        
        if (categoryId != null) {
            Optional<Category> categoryOpt = categoryService.findById(categoryId);
            if (categoryOpt.isPresent()) {
                selectedCategory = categoryOpt.get();
                documents = textDocumentService.getDocumentsByCategory(categoryId);
            } else {
                documents = textDocumentService.getFeaturedDocuments();
            }
        } else {
            documents = textDocumentService.getFeaturedDocuments();
        }
        
        model.addAttribute("documents", documents);
        model.addAttribute("selectedCategory", selectedCategory);
        
        // 获取主页背景图片
        String backgroundImage = siteSettingsService.getSettingValue("home.background", "");
        model.addAttribute("backgroundImage", backgroundImage);
        
        // 获取背景透明度
        String backgroundOpacity = siteSettingsService.getSettingValue("home.background.opacity", "0.5");
        model.addAttribute("backgroundOpacity", backgroundOpacity);
        
        return "home";
    }
    
    @GetMapping("/documents/{id}")
    public String viewDocument(@PathVariable Long id, Model model) {
        TextDocument document = textDocumentService.getDocumentById(id);
        model.addAttribute("document", document);
        
        // 获取主页背景图片和透明度设置，应用到文档详情页
        String backgroundImage = siteSettingsService.getSettingValue("home.background", "");
        model.addAttribute("backgroundImage", backgroundImage);
        
        // 获取背景透明度
        String backgroundOpacity = siteSettingsService.getSettingValue("home.background.opacity", "0.5");
        model.addAttribute("backgroundOpacity", backgroundOpacity);
        
        return "document-view";
    }

    @GetMapping("/weblinks")
    public String showWebLinks(Model model) {
        model.addAttribute("webLinks", webLinkService.getActiveWebLinks());
        model.addAttribute("pageTitle", "网站导航");
        
        // 添加网站链接分类列表到模型
        model.addAttribute("linkCategories", linkCategoryService.findAllLinkCategories());
        
        // 获取背景图片
        String backgroundImage = siteSettingsService.getSettingValue("weblinks.background", "");
        model.addAttribute("backgroundImage", backgroundImage);
        
        // 获取背景透明度
        String backgroundOpacity = siteSettingsService.getSettingValue("weblinks.background.opacity", "0.5");
        model.addAttribute("backgroundOpacity", backgroundOpacity);
        
        return "weblinks";
    }
} 